#include "StdAfx.h"
#include "Cube.h"
Cube::Cube(void)
{
	vertices[0] = Vertex(-1, 1, 1, 1, 1, 0, 0, 0, 1);
	vertices[1] = Vertex( 1, 1, 1, 1, 0, 1, 0, 1, 1);
	vertices[2] = Vertex( 1,-1, 1, 1, 0, 0, 1, 0, 0);
	vertices[3] = Vertex(-1,-1, 1, 1, 0, 1, 0, 1, 0);
	vertices[4] = Vertex(-1, 1,-1, 1, 1, 0, 0, 0, 1);
	vertices[5] = Vertex( 1, 1,-1, 1, 0, 1, 0, 1, 1);
	vertices[6] = Vertex( 1,-1,-1, 1, 0, 0, 1, 0, 0);
	vertices[7] = Vertex(-1,-1,-1, 1, 0, 1, 0, 1, 0);
	
	GLushort indices_tmp[] = {0, 3, 2, 0, 2, 1, 1, 2, 6, 1, 6, 5, 5, 6, 7, 5, 7, 4,
							4, 7, 3, 4, 3, 0, 4, 0, 1, 4, 1, 5, 7, 3, 2, 7, 2, 6};
	memcpy(indices, indices_tmp, sizeof(indices_tmp));


	// Init
	glGenBuffers(1, &elementbuffer);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void Cube::Draw( ESContext *esContext , Shaders* myShaders, Camera* camera)
{
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW);
	glEnableVertexAttribArray(myShaders->positionAttribute);
	glVertexAttribPointer(myShaders->positionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
	glEnableVertexAttribArray(myShaders->colorAtttribute);
	glVertexAttribPointer(myShaders->colorAtttribute, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char*)0 + sizeof(Vector3));
	glEnableVertexAttribArray(myShaders->uvAttribute);
	glVertexAttribPointer(myShaders->uvAttribute, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char*)0 + sizeof(Vector3) + sizeof(Vector4));

	//glBindTexture(GL_TEXTURE_2D, textureID);
	//glUniform1i(myShaders->textureUniform, 0);

	//Pass WorldMatrix
	glUniformMatrix4fv(myShaders->matViewUniform, 1, GL_FALSE, &(camera->getViewMatrix().Transpose().m[0][0]));
	glUniformMatrix4fv(myShaders->matProjectionUniform, 1, GL_FALSE, &(camera->getProjectionMatrix().Transpose().m[0][0]));
	glUniformMatrix4fv(myShaders->matWorldUniform, 1, GL_FALSE, &(worldMatrix.Transpose().m[0][0]));
	
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
	int size;
	glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
	glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
}
void Cube::Update ( ESContext *esContext, float deltaTime ){}

Cube::~Cube(void)
{
}
